class Utils {
	constructor() {
		this.baseUrl = 'http://159.75.169.224:7300/pz'
	}
	// 获取用户信息
	getUserInfo() {
		uni.login({
			success:(res) => {
				this.request({
					url: '/auth/wxLogin',
					data: { code: res.code },
					success: result => {
						console.log(result,'res');
					}
				})
			}
		})
	}
	request(option = { }) {
		let { url = '',method = 'GET',data = {}, header = {}, showLoading = true } = option
		if(!url) return 
		if(showLoading) this.showLoading()
		uni.request({
			url: this.baseUrl + url,
			data,
			method,
			header,
			success: (response) => {
				uni.hideLoading()
				// 如果返回异常
				let { code } = response.data
				if(code !== 10000) {
					if(option.fail && typeof option.fail === 'function') {
						option.fail(response)
					}
				} else {
					if(option.success && typeof option.success === 'function') {
						option.success(response.data)
					}
				}
			},
			fail(respose) {
				uni.hideLoading()
				console.error(respose);
			}
		})
	}
	showLoading() {
		const isLoading = uni.getStorageSync('isLoading')
		if(isLoading) {
			uni.hideLoading()
			uni.setStorageSync('isLoading',false)
		}
		uni.showLoading({
			title: '加载中...',
			complete() {
				uni.setStorageSync('isLoading', true)
			},
			fail() {
				uni.setStorageSync('isLoading',false)
			}
		})
	}
}

export default new Utils()